import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/eqp_info_entity.dart';
import 'package:xin_nuo_pda/http/api/eqp_api.dart';

import 'EquipmentAskRepairState.dart';

class EquipmentAskRepairController extends BaseController {
  final EquipmentAskRepairState state = EquipmentAskRepairState();

  void updateSelectDate(DateTime dateTime) {
    state.selectDate = dateTime;
    update([state.dateChooseId]);
  }

  getEqpInfo() async {
    EqpInfoEQUIPMENT eqpInfoEQUIPMENT = EqpInfoEQUIPMENT();
    eqpInfoEQUIPMENT.eQUIPMENTID = state.eqpID;
    Map<String, dynamic> params = {
      'ACTIONTYPE': 'LOAD',
      'EQUIPMENT': eqpInfoEQUIPMENT
    };
    var result = await EqpApi.eqpQuery(canelToken, params: params);
    if (result.ok) {
      state.eqpInfoEntity = result.data;
      update([state.eqpInfoId]);
    }
  }

  submit() async {
    if (state.eqpID.isEmpty) {
      Get.snackbar('提示', "请先扫描设备号",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }

    if (state.descController.text.isEmpty) {
      Get.snackbar('提示', "请输入故障描述",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }

    if (state.userController.text.isEmpty) {
      Get.snackbar('提示', "请选择操作员",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }

    if (state.selectDate.toString().isEmpty) {
      Get.snackbar('提示', "请选择故障发生时间",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }

    Map<String, dynamic> params = {
      'EQUIPMENTID': state.eqpID,
      'TROUBLECOMMENT': state.descController.text,
      'TROUBLEREASON': state.contentController.text,
      'TROUBLEREPORTER': state.userController.text,
      'TROUBLETIME': state.selectDate.year.toString() +
          "-" +
          (state.selectDate.month < 10
              ? '0' + state.selectDate.month.toString()
              : state.selectDate.month.toString()) +
          '-' +
          (state.selectDate.day < 10
              ? '0' + state.selectDate.day.toString()
              : state.selectDate.day.toString()) + ' ' + state.selectDate.hour.toString() + ":" + state.selectDate.minute.toString() + ":00",
      'RESERVED1': state.lotID,
      'EVENTID': 'IDLE-REPAIR'
    };
    var result = await EqpApi.productionRepair(canelToken, params: params);
    if (result.ok) {
      Fluttertoast.showToast(msg: '报修成功');
      Get.back();
    }
  }
}
